//
//  SaveController.m
//  MyTrip
//
//  Created by zhang on 15/9/1.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "SaveController.h"
#import "DiaryDataBase.h"
#import "SaveCell.h"
#import "SaveDetailController.h"
#import "Diary.h"


@interface SaveController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *tableArray;


@end

@implementation SaveController

- (void)dealloc
{
    [_table release];
    [_tableArray release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}


- (void)createTable{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
//    _table.bounces = NO;
//    _table.scrollEnabled = NO;
    [_table registerClass:[SaveCell class] forCellReuseIdentifier:@"save"];
    
    [self.view addSubview:_table];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tableArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 105 * OffHeight;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SaveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"save"];
    Diary *diary = [_tableArray objectAtIndex:indexPath.row];
    [cell setValueWithDiary:diary];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Diary *diary = [_tableArray objectAtIndex:indexPath.row];
    SaveDetailController *detail = [[SaveDetailController alloc] init];
    detail.diary = diary;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        Diary *diary = [_tableArray objectAtIndex:indexPath.row];
        [_tableArray removeObjectAtIndex:indexPath.row];
        [DiaryDataBase delegeteWithDiaryid:diary.diaryid];
        [_table reloadData];
    }
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:70 / 255.0f green:160 / 255.0f blue:175 / 255.0f alpha:1];
    
    self.navigationItem.title = @"我的收藏";
    [self createTable];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *array = [DiaryDataBase selectAll];
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableArray addObjectsFromArray:array];
            if (_tableArray.count == 0) {
                UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"还没有收藏哟(*^__^*) " preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    
                }];
                [alter addAction:ok];
                [self presentViewController:alter animated:YES completion:^{
                    
                }];
                
            }
            
            
            
            [_table reloadData];
        });
    });
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_tableArray removeAllObjects];
    self.tabBarController.tabBar.hidden = YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
